package myapps.LetsPlayAGame;

import com.google.gson.Gson;

/**
 * Parses, maintains, and presents the current game state.
 * 
 * The board will be represented as a string, where each position corresponds to a position on the board. Board positioning is as follows:
 * 1 2 3
 * 4 5 6     each position has a number 1-9
 * 7 8 9
 * 
 * - represents an empty space
 * c represents a space occupied by a computer piece
 * p represents a space occupied by a player piece
 *
 */
public class GameState {

	private boolean gameRunning;
	private boolean placePhase;
	private boolean movePhase;
	private int player_wins;
	private int computer_wins;
	private char[] board;
	private String hash;
	private Gson gson;

	public GameState(){
		hash = null;
		gameRunning = false;
		placePhase = false;
		movePhase = false;
		player_wins = 0;
		computer_wins = 0;
		board = new char[]{'-', '-', '-', '-', '-', '-', '-', '-', '-'};
		gson = new Gson();
	}
	
	/**
	 * Parses the json response and sets the current game state.
	 * @param stringResponse
	 * @return
	 */
	public String update(String stringResponse) {
		int freeSpaces = 0;
		
		Response response = gson.fromJson(stringResponse, Response.class);
		player_wins = response.data.player_wins;
		computer_wins = response.data.computer_wins;
		hash = response.data.hash;
		
		//Check for a board state in the response
		if(response.data.board == null){
			board = null;
			gameRunning = false;
		}
		else{
			//Determine if the game is still running
			if(response.status.equals("success")){
				gameRunning = true;
			}
			else{
				gameRunning = false;
			}
			
			board = response.data.board.toCharArray();
			
			//Basic validation check for length
			if(board.length != 9){
				//invalid length
				throw new RuntimeException("Failed : Invalid board length : \n" + gson.toJson(response));
			}
			else{
				for(int i = 0; i<9; i++){
					if(board[i] == '-')
						freeSpaces++;
				}
				if (freeSpaces > 3){
					placePhase = true;
					movePhase = false;
				}
				else{
					placePhase = false;
					movePhase = true;
				}
			}
		}
		
		//System.out.println(gson.toJson(response));
		return gson.toJson(response);
	}

	/**
	 * String output of state
	 * @return
	 */
	public String getState() {
		StringBuilder state = new StringBuilder();
		state.append("player_wins = " + player_wins + "\n");
		state.append("computer_wins = " + computer_wins + "\n");
		state.append("gameRunning = " + gameRunning + "\n");
		state.append("placePhase = " + placePhase + "\n");
		state.append("movePhase = " + movePhase + "\n");
		state.append("hash = " + hash + "\n");
		state.append("Board : " + "\n");
		state.append("" + board[0] + board[1] + board[2] + "\n");
		state.append("" + board[3] + board[4] + board[5] + "\n");
		state.append("" + board[6] + board[7] + board[8] + "\n");
		state.append("\n");
		return state.toString();
	}

	public boolean isGameRunning() {
		return gameRunning;
	}

	public boolean isPlacePhase() {
		return placePhase;
	}
	
	public boolean isMovePhase() {
		return movePhase;
	}

	public String getHash() {
		return hash;
	}
	
	@Override
	public String toString(){
		return getState();
	}

	public String getBoard() {
		return String.valueOf(board);
	}
}
